package com.example.concurrency.chapter8;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ExecutorsTests {
    public static void main(String[] args) throws InterruptedException {
        Executors.newCachedThreadPool();
        Executors.newFixedThreadPool(10);
        ExecutorService executor = Executors.newSingleThreadExecutor();
        executor.execute(new Task());

        Thread.sleep(3000);
        System.out.println("shutdown threadPoolExecutor");
        executor.shutdown();

        Thread.sleep(3000);
        System.out.println("shutdownNoe threadPoolExecutor");
        executor.shutdownNow();
    }

    static class Task implements Runnable{
        @Override
        public void run() {
            for (int i = 0; i < 10; i++){
                try {
                    System.out.println(Thread.currentThread().getName() + ": " + i);
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    System.out.println(Thread.currentThread().getName() + ": 线程被中断");
                    break;
                }
            }
        }
    }
}
